/**
 * 
 */
package arisufascion.com.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import arisufascion.com.AbstractActivity;
import arisufascion.com.R;
import arisufascion.com.adapter.CategoriesAdapter;
import arisufascion.com.constant.Constants;
import arisufascion.com.constant.WriteLog;
import arisufascion.com.handler.CategorieHandler;
import arisufascion.com.handler.ServiceHandler;
import arisufascion.com.model.Categorie;
import arisufascion.com.network.RequestTask;
import arisufascion.com.tabs.TabGroupActivity;

/**
 * @author Kelvin
 *
 */
public class ActivityCategories extends AbstractActivity {
	
	private List<Categorie> categories=new ArrayList<Categorie>();
	private CategoriesAdapter mAdapter;
	private ProgressDialog dialog;
	private GridView gridView_Categories;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_categories);
		
		findViewById(R.id.btn_menu).setOnClickListener(onClick);
		findViewById(R.id.btn_privacy).setOnClickListener(onClick);
		findViewById(R.id.btn_wholesale).setOnClickListener(onClick);
		findViewById(R.id.btn_shipping).setOnClickListener(onClick);
		
		
		gridView_Categories=(GridView)findViewById(R.id.gridView_Categories);
		
		gridView_Categories.setOnItemClickListener(onItemClick);
		
		if (isNetworkAvailable()) {
			if (categories.size() < 1) {
				new getCategoriesFromServer().execute();
			}
		} else {
			Toast.makeText(self, "Please On wifi or 3G.", Toast.LENGTH_LONG)
					.show();
			self.finish();
		}
		
	}
	
	public class getCategoriesFromServer extends AsyncTask<Void, Void, List<Categorie>> {
		int number_file=1;
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Please wait...");
			dialog.show();
		}

		@Override
		protected List<Categorie> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String url = Constants.SERVER_URL_CATEGORIES;
			url = String.format(url, Constants.SERVER_API_KEY);

			WriteLog.d(TAG, url);
			String json = RequestTask.getJsonFromUrl(url);
			
			ServiceHandler service = new ServiceHandler();
			CategorieHandler mCategorieHandler= service.getCategories(json);
			
			return mCategorieHandler.getData();

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(List<Categorie> result) {
			super.onPostExecute(result);
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (result != null) {
				mAdapter = new CategoriesAdapter(ActivityCategories.this, result);
				gridView_Categories.setAdapter(mAdapter);
			}
		}

	}
	OnItemClickListener onItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long arg3) {
			
			Categorie mCategorie = (Categorie) parent.getAdapter().getItem(position);
			Bundle bundle = new Bundle();
			bundle.putInt(CATEGORIE_ID, mCategorie.getVirtuemart_category_id());

			Intent intent_product = new Intent(self, ActivityProducts.class);
			intent_product.putExtras(bundle);
//			startActivity(intent_product);
			
			TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
			tabGroupActivity.startChildActivity("ActivityProducts",
					intent_product);
		}
	};
	
}
